package cn.jhc.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.jhc.bean.HfmsgBean;
import cn.jhc.bean.UserBean;
import cn.jhc.dao.ConnectionFactory;
import cn.jhc.dao.MsgDao;

@WebServlet(name = "Get HF detail", urlPatterns = { "/addHfmessage.do" })
public class HfMsgServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		UserBean user = (UserBean) req.getSession().getAttribute("user");
		if (null == user) {
			String getb = (String) req.getParameter("getb");
			String id = (String) req.getParameter("id");
			req.setAttribute("id", id);
			req.setAttribute("getb", getb);
			RequestDispatcher dispatcher = req
					.getRequestDispatcher("/login.jsp");
			dispatcher.forward(req, resp);
			return;
		}
		req.setCharacterEncoding("UTF-8");
		String content = (String) req.getParameter("content");
		String Titleid = (String) req.getParameter("titleid");
		if(content==""||content.equals("内容不能为空")){
			System.out.println("1111");
			String id =(String)req.getParameter("id");
			req.setAttribute("id", id);
			RequestDispatcher dispatcher = req.getRequestDispatcher("/showmessages.jsp");
			dispatcher.forward(req, resp);
			return;
			
		}
		int titleid = Integer.parseInt(Titleid);
		int userid = user.getId();	
		MsgDao dao = new MsgDao();
		dao.setCon(ConnectionFactory.getConnection());
		HfmsgBean m = new HfmsgBean();
		dao.addHfMessage(userid, content, titleid);
		m = dao.getoneMessages(titleid);
		req.setAttribute("title", m);
		
		RequestDispatcher dispatcher = req
				.getRequestDispatcher("/chonggong2.jsp");
		dispatcher.forward(req, resp);
	}
}